﻿using System;
using System.Collections.Generic;
using UwaySoft.EsheetServer.DataLayer;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using System.Xml;
using System.Configuration;
using UwaySoft.EsheetServer.BizLogic;

namespace UwaySoft.EsheetServer.Help
{
    public class SendReplayToService
    {
        protected ISqlHelper SqlHelper
        {
            get { return Application.SqlHelper; }
        }

        public string ReplyAffirmInfo(int type, string serialNo, string replayInfo,
                                        string userName, string userPhone1, string userPhone2,
                                        string dept, string deptAndDate, string remark)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<?xml version=\"1.0\" encoding=\"gb2312\"?>");
            sb.Append("<replyAffirm>");
            sb.Append("   <explain>网优平台工单系统返回确认单给客服</explain>");
            sb.Append("   <affirmInfo>");
            sb.Append("     <type>" + type + "</type>");
            sb.Append("     <serialnfoNo>" + serialNo + "</serialnfoNo>");
            sb.Append("     <Acceptdea>" + replayInfo + "</Acceptdea>");
            sb.Append("     <Accepter>" + userName + "</Accepter>");
            sb.Append("     <LinkTel1>" + userPhone1 + "</LinkTel1>");
            sb.Append("     <LinkTel2>" + userPhone2 + "</LinkTel2>");
            sb.Append("     <dutyOrga>" + dept + "</dutyOrga>");
            sb.Append("     <toOtherDeptAndTime>" + deptAndDate + "</toOtherDeptAndTime>");
            sb.Append("     <remark>" + remark + "</remark>");
            sb.Append("     <reservation1/>");
            sb.Append("     <reservation2/>");
            sb.Append("     <reservation3/>");
            sb.Append("     <reservation4/>");
            sb.Append("   </affirmInfo>");
            sb.Append("</replyAffirm>");
            return sb.ToString();
        }

        public bool CallService(int type, string serialNo, string replayInfo,
                                        string loginName, string deptAndDate, ref string remark)
        {
            string sqlTemp = string.Format("select u.username,u.utelephone,u.umobilecode,s.deptname from SEC_USERINFO u " +
                                            "left join sec_dept s on u.deptid = s.id " +
                                             "where u.loginname = '{0}'", loginName);

            DataTable dtUser = null;
            DataSet ds = SqlHelper.ExecuteDateSet(sqlTemp);
            if (ds != null && ds.Tables.Count > 0)
            {
                dtUser = ds.Tables[0];
            }

            //如果找不到数据抛异常
            if (dtUser == null || dtUser.Rows.Count == 0)
                throw new Exception(string.Format("数据错误，找不到登录名『{0}』的用户数据", loginName));

            string userName = dtUser.Rows[0]["username"].ToString();
            string tel1 = dtUser.Rows[0]["utelephone"].ToString();
            string tel2 = dtUser.Rows[0]["umobilecode"].ToString();
            string dept = dtUser.Rows[0]["deptname"].ToString();

            if (!string.IsNullOrEmpty(tel1) && tel1.IndexOf(";") > 0)
            {
                tel1 = tel1.Substring(0, 11);
            }
            if (!string.IsNullOrEmpty(tel2) && tel2.IndexOf(";") > 0)
            {
                tel2 = tel2.Substring(0, 11);
            }

            string sendInfo = this.ReplyAffirmInfo(type, serialNo, replayInfo, userName, tel1, tel2, dept, deptAndDate, remark);

            Hashtable ht = new Hashtable();
            ht.Add(ConfigurationManager.AppSettings["argName"], sendInfo);
            string getXml = WebSvcCaller.QuerySoapWebService(ConfigurationManager.AppSettings["esheetServer.replyAffirm"], "replyAffirm", ht).InnerText;
            if (getXml == null)
                return false;
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(getXml);

            string result = doc.SelectSingleNode("//returnNo").InnerText;
            if (result == "0")
                return true;
            else
            {
                remark = doc.SelectSingleNode("//remark").InnerText;
                return false;
            }
        }
    }
}
